home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / c13 / Menu1.java < prev    next >
Encoding:
Java Source  |  2000-05-25  |  5.3 KB  |  151 lines

  1. //: Menu1.java
  2. //////////////////////////////////////////////////
  3. // Copyright (c) Bruce Eckel, 1998
  4. // Source code file from the book "Thinking in Java"
  5. // All rights reserved EXCEPT as allowed by the
  6. // following statements: You can freely use this file
  7. // for your own work (personal or commercial),
  8. // including modifications and distribution in
  9. // executable form only. Permission is granted to use
  10. // this file in classroom situations, including its
  11. // use in presentation materials, as long as the book
  12. // "Thinking in Java" is cited as the source. 
  13. // Except in classroom situations, you cannot copy
  14. // and distribute this code; instead, the sole
  15. // distribution point is http://www.BruceEckel.com 
  16. // (and official mirror sites) where it is
  17. // freely available. You cannot remove this
  18. // copyright and notice. You cannot distribute
  19. // modified versions of the source code in this
  20. // package. You cannot use this file in printed
  21. // media without the express permission of the
  22. // author. Bruce Eckel makes no representation about
  23. // the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied
  25. // warranty of any kind, including any implied
  26. // warranty of merchantability, fitness for a
  27. // particular purpose or non-infringement. The entire
  28. // risk as to the quality and performance of the
  29. // software is with you. Bruce Eckel and the
  30. // publisher shall not be liable for any damages
  31. // suffered by you or any third party as a result of
  32. // using or distributing software. In no event will
  33. // Bruce Eckel or the publisher be liable for any
  34. // lost revenue, profit, or data, or for direct,
  35. // indirect, special, consequential, incidental, or
  36. // punitive damages, however caused and regardless of
  37. // the theory of liability, arising out of the use of
  38. // or inability to use software, even if Bruce Eckel
  39. // and the publisher have been advised of the
  40. // possibility of such damages. Should the software
  41. // prove defective, you assume the cost of all
  42. // necessary servicing, repair, or correction. If you
  43. // think you've found an error, please email all
  44. // modified files with clearly commented changes to:
  45. // Bruce@EckelObjects.com. (Please use the same
  46. // address for non-code errors found in the book.)
  47. /////////////////////////////////////////////////
  48.  
  49. // Menus work only with Frames.
  50. // Shows submenus, checkbox menu items
  51. // and swapping menus.
  52. import java.awt.*;
  53.  
  54. public class Menu1 extends Frame {
  55.   String[] flavors = { "Chocolate", "Strawberry",
  56.     "Vanilla Fudge Swirl", "Mint Chip", 
  57.     "Mocha Almond Fudge", "Rum Raisin", 
  58.     "Praline Cream", "Mud Pie" };
  59.   TextField t = new TextField("No flavor", 30);
  60.   MenuBar mb1 = new MenuBar();
  61.   Menu f = new Menu("File");
  62.   Menu m = new Menu("Flavors");
  63.   Menu s = new Menu("Safety");
  64.   // Alternative approach:
  65.   CheckboxMenuItem[] safety = {
  66.     new CheckboxMenuItem("Guard"),
  67.     new CheckboxMenuItem("Hide")
  68.   };
  69.   MenuItem[] file = {
  70.     new MenuItem("Open"),
  71.     new MenuItem("Exit")
  72.   };
  73.   // A second menu bar to swap to:
  74.   MenuBar mb2 = new MenuBar();
  75.   Menu fooBar = new Menu("fooBar");
  76.   MenuItem[] other = {
  77.     new MenuItem("Foo"),
  78.     new MenuItem("Bar"),
  79.     new MenuItem("Baz"),
  80.   };
  81.   Button b = new Button("Swap Menus");
  82.   public Menu1() {
  83.     for(int i = 0; i < flavors.length; i++) {
  84.       m.add(new MenuItem(flavors[i]));
  85.       // Add separators at intervals:
  86.       if((i+1) % 3 == 0) 
  87.         m.addSeparator();
  88.     }
  89.     for(int i = 0; i < safety.length; i++)
  90.       s.add(safety[i]);
  91.     f.add(s);
  92.     for(int i = 0; i < file.length; i++)
  93.       f.add(file[i]);
  94.     mb1.add(f);
  95.     mb1.add(m);
  96.     setMenuBar(mb1);
  97.     t.setEditable(false);
  98.     add("Center", t);
  99.     // Set up the system for swapping menus:
  100.     add("North", b);
  101.     for(int i = 0; i < other.length; i++)
  102.       fooBar.add(other[i]);
  103.     mb2.add(fooBar);
  104.   }
  105.   public boolean handleEvent(Event evt) {
  106.     if(evt.id == Event.WINDOW_DESTROY) 
  107.       System.exit(0);
  108.     else 
  109.       return super.handleEvent(evt);
  110.     return true;
  111.   }
  112.   public boolean action(Event evt, Object arg) {
  113.     if(evt.target.equals(b)) {
  114.       MenuBar m = getMenuBar();
  115.       if(m == mb1) setMenuBar(mb2);
  116.       else if (m == mb2) setMenuBar(mb1);
  117.     } 
  118.     else if(evt.target instanceof MenuItem) {
  119.       if(arg.equals("Open")) {
  120.         String s = t.getText();
  121.         boolean chosen = false;
  122.         for(int i = 0; i < flavors.length; i++)
  123.           if(s.equals(flavors[i])) chosen = true;
  124.         if(!chosen)
  125.           t.setText("Choose a flavor first!");
  126.         else
  127.           t.setText("Opening "+ s +". Mmm, mm!");
  128.       }
  129.       else if(evt.target.equals(file[1]))
  130.         System.exit(0);
  131.       // CheckboxMenuItems cannot use String 
  132.       // matching; you must match the target:
  133.       else if(evt.target.equals(safety[0]))
  134.         t.setText("Guard the Ice Cream! " +
  135.           "Guarding is " + safety[0].getState());
  136.       else if(evt.target.equals(safety[1]))
  137.         t.setText("Hide the Ice Cream! " +
  138.           "Is it cold? " + safety[1].getState());
  139.       else 
  140.         t.setText(arg.toString());
  141.     } 
  142.     else 
  143.       return super.action(evt, arg);
  144.     return true;
  145.   }
  146.   public static void main(String[] args) {
  147.     Menu1 f = new Menu1();
  148.     f.resize(300,200);
  149.     f.show();
  150.   }
  151. } ///:~